package com.lv.pc.viewpagerphotocarousel;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.Log;

/**
 * Created by pc on 2014/12/14.
 */
public class ViewPagerThread extends Thread {
    private ViewPager viewPager;
    private int index=0;
    private Activity activity;
    private int indexMax;
    public ViewPagerThread(ViewPager viewPager,int index,int indexMax,Context context){
        this.viewPager = viewPager;
        this.index = index;
        this.activity = (Activity)context;
        this.indexMax = indexMax;
    }
    @Override
    public void run() {
        try {
            while (true){
                Thread.sleep(8000);
                setIndex(index+1);
                if (index==indexMax){
                    index=0;
                }
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        viewPager.setCurrentItem(index);
                    }
                });
            }
        }catch (Exception e){
            Log.v("lvzhongyi","run is error");
        }
    }
    public synchronized void setIndex(int positon){
        this.index = positon;
    }
}
